<?php
class campusModel extends model
{
    public function create()
    {
        try{
            $campus = fixer::input('post')
                ->specialchars('name')
                ->add('date', date('Y-m-d H:i:s'))->get();
            $this->dao->insert('campus')->data($campus)
                ->batchCheck('name', 'notempty')->exec();
            return $this->dao->lastInsertID();
        } catch (Exception $e) {
            var_dump($e->getMessage());
        }
    }
    public function delete($id, $table = null)
    {
        // TODO：校区删除检查，如果校区内还有学生，则不允许删除校区
        $hasStudent = $this->dao->select('id')
            ->from("student_campus")
            ->where("campus_id")->eq($id)
            ->fetch();
        if($hasStudent != false) {
            return false;
        }else {
            return $this->dao->update("campus")->set('del=1')->where('id')->eq($id)->exec();
        }
    }
    public function getList($pager = null)
    {
        return $this->dao->select("*")
            ->from('campus')
            ->where('del')->eq("0")
            ->orderBy('id asc')
            ->page($pager)
            ->fetchAll();
    }
    public function getById($id)
    {
        return $this->dao->select("*")
            ->from('campus')
            ->where('id')->eq($id)
            ->fetch();
    }
    public function getChild($id)
    {
        return $this->dao->select("*")
            ->from('campus')
            ->where('parent_id')->eq($id)
            ->fetchAll();
    }
    public function update($id)
    {
        $campus = fixer::input('post')
            ->specialchars('id, name')
            ->get();
        return $this->dao->update('campus')
            ->data($campus)
            ->where('id')->eq($id)
            ->batchCheck('id, name', 'notempty')
            ->exec();
    }
}